<template>
	<view class="list-view">
		<view class="top-bar"><uni-navbar :show="show" @click-show="show = !show" :fixed="true" :back="true" color="#fff" title="统计分析"></uni-navbar></view>

		<view class="filter-box" v-if="show">
			<view class="form-box">
				<u-form ref="uForm">
					<u-form-item class="form-item" label="届次">
						<u-input v-model="jieci" type="select" :select-open="jcShow" placeholder="请选择届次" @click="jcShow = true" />
						<u-select v-model="jcShow" mode="mutil-column-auto" :list="jcList" @confirm="jcConfirm"></u-select>
					</u-form-item>
					<u-form-item class="form-item" label="年度">
						<date-time-picker placeholder="请选择年度" :defaultValue="statistics.year" fields="year" :old="true" @change="yearChange"></date-time-picker>
					</u-form-item>
				</u-form>
			</view>
			<view class="fiexd-btm">
				<view class="flex-box">
					<u-button class="form-btn" type="default" @click="resert">清除</u-button>
					<u-button class="form-btn" type="error" @click="filter">确定</u-button>
				</view>
			</view>
		</view>

		<u-tabs class="uTabs" :list="tabsList" bgColor="#e5231a" inactiveColor="#fff" activeColor="#fff" barWidth="114" :current="tabsCurrent" @change="tabsChange" :is-scroll="false"></u-tabs>

		<view class="tab-box">
			<view class="title">{{ jieci }}</view>
			<view class="search-filter" @click="show = !show"><view>筛选</view></view>
		</view>

		<view class="table-box" v-if="tabsCurrent == 0">
			<u-table class="table-inner" border-color="#F8F8F8" :key="tabIndex">
				<u-tr>
					<u-th width="300rpx">类别</u-th>
					<u-th>件数</u-th>
					<u-th>采用</u-th>
				</u-tr>
				<view class="table-cell" v-for="(item, index) of statisticsList" :key="index" @click="navTo(item)">
					<u-tr>
						<u-td width="300rpx">{{ item.typename }}</u-td>
						<u-td>
							<text class="clamp">{{ item.allcount }}</text>
						</u-td>
						<u-td>{{ item.cycount }}</u-td>
					</u-tr>
				</view>
			</u-table>
		</view>

		<view class="table-box" v-if="tabsCurrent == 1">
			<u-table class="table-inner" border-color="#F8F8F8" :key="tabIndex">
				<u-tr>
					<u-th width="450rpx">采用方式</u-th>
					<u-th>件数</u-th>
				</u-tr>
				<view class="table-cell" v-for="(item, index) of statisticsList" :key="index">
					<u-tr>
						<u-td width="450rpx">{{ item.typename }}</u-td>
						<u-td>{{ item.count }}</u-td>
					</u-tr>
				</view>
			</u-table>
		</view>

		<view class="table-box" v-if="tabsCurrent == 2">
			<u-table class="table-inner" border-color="#F8F8F8" :key="tabIndex">
				<u-tr>
					<u-th width="450rpx">类型</u-th>
					<u-th>件数</u-th>
				</u-tr>
				<view class="table-cell" v-for="(item, index) of statisticsList" :key="index">
					<u-tr>
						<u-td width="450rpx">{{ item.typename }}</u-td>
						<u-td>{{ item.count }}</u-td>
					</u-tr>
				</view>
			</u-table>
		</view>

		<view class="table-box" v-if="tabsCurrent == 3">
			<u-table class="table-inner" border-color="#F8F8F8" :key="tabIndex">
				<u-tr>
					<u-th width="450rpx">热词</u-th>
					<u-th>统计数</u-th>
				</u-tr>
				<view class="table-cell" v-for="(item, index) of statisticsList" :key="index">
					<u-tr>
						<u-td width="450rpx">{{ item.name }}</u-td>
						<u-td>{{ item.freq }}</u-td>
					</u-tr>
				</view>
			</u-table>
		</view>

		<u-loadmore :status="loading" />
		<u-back-top :scroll-top="scrollTop" top="200"></u-back-top>
	</view>
</template>

<script>
import dateTimePicker from '@/components/uni-datetime-picker';
import { mapState } from 'vuex';
export default {
	components: {
		dateTimePicker
	},
	data() {
		return {
			scrollTop: 0,
			show: false,
			tabIndex: 10,
			tabsCurrent: 0,
			tabsList: [
				{
					name: '类别统计',
					value: 0
				},
				{
					name: '采用统计',
					value: 1
				},
				{
					name: '反馈统计',
					value: 2
				},
				{
					name: '热词统计',
					value: 3
				}
			],
			jieci: '',
			jcShow: false,
			jcList: [],
			statisticsList: [],

			statistics: {
				limitNum: '',
				pagemarker: '',
				jieId: '',
				ciId: '',
				year: ''
			},
			loading: 'loadmore'
		};
	},
	onLoad() {
		this.loadLogin();
		this.loadData();
		this.jieciData();
	},
	onReachBottom() {
		if (this.loading == 'loadmore') this.loadData('add');
	},
	onPageScroll(res) {
		this.scrollTop = res.scrollTop;
	},
	computed: {
		...mapState(['userInfo'])
	},
	methods: {
		loadLogin() {
			if (this.userInfo.Authorization) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.userId = this.userInfo.sysUser.id;

				this.pageNum = 1;
				this.pageSize = 15;
			} else {
				this.$logout();
			}
		},
		loadData(type = 'add') {
			if (this.loading == 'loading') {
				return false;
			}
			if (type == 'add') {
				if (this.loading == 'nomore') {
					return false;
				}
				this.loading = 'loading';
			} else {
				this.loading = 'loading';
			}
			if (type != 'add') {
				this.pageNum = 1;
			}
			let url;
			switch (this.tabIndex) {
				case 20:
					url = '/sheqingminyi/sqmy/reportingmethod';
					break;
				case 30:
					url = '/sheqingminyi/sqmy/feedback';
					break;
				case 40:
					url = '/web/lucenesearch/hotWords';
					this.statistics.limitNum = '15';
					this.statistics.pagemarker = '社情民意';
					break;
				default:
					url = '/sheqingminyi/sqmy/classification';
			}
			let data = {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				currentUserId: this.userId,
				clientCode: this.clientCode,
				...this.statistics
			};
			this.$request(url, data, {
				load: true
			}).then((res) => {
				let count = 0;
				if (type != 'add') {
					uni.stopPullDownRefresh();
					this.statisticsList = [];
				}
				if (!res.data) {
					this.loading = 'nodata';
					return false;
				}
				this.loading = this.pageNum < Math.ceil(res.data.total / this.pageSize) ? 'loadmore' : 'nomore';
				this.pageNum = this.pageNum + 1;
				if (res.data.returnList) {
					this.statisticsList = this.statisticsList.concat(res.data.returnList);
				} else {
					this.statisticsList = this.statisticsList.concat(res.data.records.社情民意);
				}

				this.loaded = true;
				if (this.statisticsList.length <= 0) {
					this.loading = 'nodata';
				}
			});
		},
		tab(e) {
			this.tabIndex = e;
			this.loadData('refresh');
		},
		tabsChange(index) {
			this.tabsCurrent = index;
			this.resert();
		},
		yearChange({ date }) {
			this.statistics.year = date.year.toString();
		},
		jcConfirm(e) {
			this.jieci = e[0].label + e[1].label;
			this.statistics.jieId = e[0].value;
			this.statistics.ciId = e[1].value;
		},
		filter() {
			this.show = false;
			this.tabIndex = this.tabsCurrent * 10 + 10;
			this.statisticsList = [];
			this.loadData('refresh');
		},
		resert() {
			this.show = false;
			this.jieci = '';
			this.statistics.jieId = '';
			this.statistics.ciId = '';
			this.statistics.year = '';
			this.tabIndex = this.tabsCurrent * 10 + 10;
			this.statisticsList = [];
			this.loadData('refresh');
		},
		jieciData() {
			let data = {
				clientCode: this.clientCode,
				jieCi: {
					pid: ''
				}
			};
			this.$request('/jie-ci/list', data).then((res) => {
				this.jcList = [];
				let jieArr = [];
				let ciArr = [];
				res.data.forEach((item) => {
					data.jieCi.pid = item.id;
					this.$request('/jie-ci/list', data).then((res) => {
						ciArr = [];
						res.data.forEach((key) => {
							ciArr.push({
								value: key.id,
								label: key.name
							});
						});
						this.jcList.push({
							value: item.id,
							label: item.name,
							children: ciArr
						});
					});
				});
			});
		},
		navTo(item, arr = []) {
			let url;
			switch (this.tabIndex) {
				case 20:
					url = '/pagesB/will/list_statistics?type=' + item.type;
					break;
				case 30:
					url = '/pagesB/will/list_statistics?type=' + item.type;
					break;
				default:
					url = '/pagesB/will/list_statistics?type=' + item.type;
			}
			if (arr.length > 1) {
				url = url + '&' + arr[0] + '=' + arr[1];
			}
			uni.navigateTo({
				url: url
			});
		},
		detailTo(item) {
			uni.navigateTo({
				url: '/pagesB/will/detail?id=' + item.id
			});
		}
	}
};
</script>

<style lang="scss">
.tab-box {
	padding: 18rpx 0;
	font-size: 14px;
	color: #666;
	line-height: 60rpx;
	overflow-x: auto;
	background-color: #fff;

	& + .tab-box {
		padding-top: 0;
	}

	.search-filter {
		margin: 0 30rpx;
		width: 120rpx;
		height: 60rpx;
		float: right;
		text-align: center;
		border-radius: 6rpx;
		background-color: #e6e7e8;
	}

	.title {
		padding: 0 30rpx;
		float: left;
		font-weight: bold;
	}
}

.tab-nav {
	padding: 0 20rpx;
	border: 0;
	background-color: #fff;

	&:after {
		content: '';
		flex-shrink: 0;
		width: 20rpx;
		height: 1rpx;
	}

	.tab-item {
		flex-shrink: 0;
		margin: 0 10rpx;
		padding: 0 10rpx;
		height: 60rpx;
		color: #666;
		text-align: center;
		border-radius: 6rpx;
		background-color: #e6e7e8;
	}

	.tab-item.active {
		font-weight: bold;
		color: #fff;
		background-color: #e5231a;
	}
}

.filter-box {
	position: fixed;
	left: 0;
	top: 0;
	bottom: 0;
	right: 0;
	z-index: 998;
	background-color: #fff;
}

.table-box {
	clear: both;

	.name {
		display: flex;
		flex-wrap: nowrap;
		justify-content: center;

		text {
			color: #333;
			width: 260rpx;
			text-align: left;
		}
	}

	text {
		color: #0196fa;
	}

	.break-all {
		word-break: break-all;
	}

	image {
		margin-right: 20rpx;
		width: 80rpx;
		height: 80rpx;
	}
}

.form-box {
	.form-item {
		@include mp-flex;
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}
</style>
